//
//  XKMineLevelUpConfirmController.m
//  XKJY
//
//  Created by xiaokang100 on 2017/2/10.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKMineLevelUpConfirmController.h"
#import "LevelUpView.h"

#import "XKRechargeController.h"
#import "XKTradeController.h"


@interface XKMineLevelUpConfirmController ()
@property (nonatomic, strong) LevelUpView *levelUpView;
@property (nonatomic, strong) UIBarButtonItem *rightButton;
@end

@implementation XKMineLevelUpConfirmController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self initSubviews];
}

- (void)initSubviews {
    
    
    _rightButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(rechargeMoneyOrWos:)];
    self.navigationItem.rightBarButtonItem = _rightButton;
    
    
    _levelUpView = [LevelUpView new];
    _levelUpView.frame = self.view.bounds;
    _levelUpView.titleString = @"尊敬的用户您好!";
    
    if ([self.levelUPType isEqualToString:@"1"]) {
        // 人民币升级
        /**********************************************************************************************************************/
        
        // 现有人民币
        NSString *rmb_have = [NSString changeNumberFormat:[UserDefaults objectWithKey:@"rmb"]];
        
        // 升级所需人民币
        NSString *rmb_content_vip = [NSString stringWithFormat:@"申请小康会员需要支付200.00元人民币,您目前的可用人民币资产为%@元",rmb_have];
        NSString *rmb_content_xfs = [NSString stringWithFormat:@"申请消费商需要支付5000.00元人民币,您目前的可用人民币资产为%@元",rmb_have];
        NSString *rmb_content_jpxfs = [NSString stringWithFormat:@"申请金牌消费商需要支付50000.00元人民币,您目前的可用人民币资产为%@元",rmb_have];
        
        /**********************************************************************************************************************/
        
        switch (self.level) {
            case 1: {
                if (rmb_have.doubleValue < 200) {
                    _levelUpView.contentString = [NSString stringWithFormat:@"%@,请点击右上角充值,成功后返回我的页面再次点击快速升级",rmb_content_vip];
                    _levelUpView.available = NO;
                    [_rightButton setTitle:@"充值"];
                } else {
                    _levelUpView.contentString = rmb_content_vip;
                    _levelUpView.available = YES;
                    [_rightButton setTitle:@""];
                }
            } break;
                
                
            case 2: {
                if (rmb_have.doubleValue < 5000) {
                    _levelUpView.contentString = [NSString stringWithFormat:@"%@,请点击右上角充值,成功后再次点击快速升级通道",rmb_content_xfs];
                    _levelUpView.available = NO;
                    [_rightButton setTitle:@"充值"];
                } else {
                    _levelUpView.contentString = rmb_content_xfs;
                    _levelUpView.available = YES;
                    [_rightButton setTitle:@""];
                }
            } break;
                
                
            case 3: {
                if (rmb_have.doubleValue < 50000) {
                    _levelUpView.contentString = [NSString stringWithFormat:@"%@,请点击右上角充值,成功后再次点击快速升级通道",rmb_content_jpxfs];
                    _levelUpView.available = NO;
                    [_rightButton setTitle:@"充值"];
                } else {
                    _levelUpView.contentString = rmb_content_jpxfs;
                    _levelUpView.available = YES;
                    [_rightButton setTitle:@""];
                }
            } break;
                
            default:
                break;
        }
        
    } else if ([self.levelUPType isEqualToString:@"2"]) {
        
        
        // 种子升级
        /**********************************************************************************************************************/
        NSString *new_price = [UserDefaults objectWithKey:@"new_price"];
        if (new_price.doubleValue == 0.00) {
            new_price = @"1";
        }
        // 升级所需种子
        NSString *wos_need_vip = [NSString calculateNumberOne:@"200" calculateType:CalculateType_Dividing numberTwo:new_price withMode:NSRoundUp scale:0];
        NSString *wos_need_xfs = [NSString calculateNumberOne:@"5000" calculateType:CalculateType_Dividing numberTwo:new_price withMode:NSRoundUp scale:0];
        NSString *wos_need_jpxfs = [NSString calculateNumberOne:@"50000" calculateType:CalculateType_Dividing numberTwo:new_price withMode:NSRoundUp scale:0];
        
        // 现有种子
        NSString *wos_have_temp = [UserDefaults objectWithKey:@"wopnum"];
        NSString *wos_have = [NSString stringWithFormat:@"%zd",wos_have_temp.integerValue];
        
        NSString *wos_content_vip = [NSString stringWithFormat:@"申请小康会员需要消耗%@个小康种子(WOS),您目前的小康种子为%@个",[NSString wosNumberFormat:wos_need_vip], [NSString wosNumberFormat:wos_have]];
        NSString *wos_content_xfs = [NSString stringWithFormat:@"申请消费商需要消耗%@个小康种子(WOS),您目前的小康种子为%@个",[NSString wosNumberFormat:wos_need_xfs], [NSString wosNumberFormat:wos_have]];
        NSString *wos_content_jpxfs = [NSString stringWithFormat:@"申请金牌消费商需要消耗%@个小康种子(WOS),您目前的小康种子为%@个",[NSString wosNumberFormat:wos_need_jpxfs], [NSString wosNumberFormat:wos_have]];
        
        /**********************************************************************************************************************/
        
        switch (self.level) {
            case 1: {
                if (wos_have.integerValue < wos_need_vip.integerValue) {
                    _levelUpView.contentString = [NSString stringWithFormat:@"%@,请点击右上角购买小康种子",wos_content_vip];
                    _levelUpView.available = NO;
                    [_rightButton setTitle:@"购买"];
                } else {
                    _levelUpView.contentString = wos_content_vip;
                    _levelUpView.available = YES;
                    [_rightButton setTitle:@""];
                }
            } break;
                
                
            case 2: {
                if (wos_have.integerValue < wos_need_xfs.integerValue) {
                    _levelUpView.contentString = [NSString stringWithFormat:@"%@,请点击右上角购买小康种子",wos_content_xfs];
                    _levelUpView.available = NO;
                    [_rightButton setTitle:@"购买"];
                } else {
                    _levelUpView.contentString = wos_content_xfs;
                    _levelUpView.available = YES;
                    [_rightButton setTitle:@""];
                }
            } break;
                
                
            case 3: {
                if (wos_have.integerValue < wos_need_jpxfs.integerValue) {
                    _levelUpView.contentString = [NSString stringWithFormat:@"%@,请点击右上角购买小康种子",wos_content_jpxfs];
                    _levelUpView.available = NO;
                    [_rightButton setTitle:@"购买"];
                } else {
                    _levelUpView.contentString = wos_content_jpxfs;
                    _levelUpView.available = YES;
                    [_rightButton setTitle:@""];
                }
            } break;
                
            default:
                break;
        }
    }
    
    
    @weakify(self);
    _levelUpView.levelUpBlock = ^(NSString *invitePhone) {
        @strongify(self);
        [self goTolevelUpWithInvitePhone:invitePhone];
    };
    
    
    [self.view addSubview:self.levelUpView];
}


#pragma mark - rechargeMoneyOrWos
- (void)rechargeMoneyOrWos:(UIBarButtonItem *)sender {
    
    NSString *str = sender.title;
    if ([str isEqualToString:@"充值"]) {

        XKRechargeController *rechargeVC = [[XKRechargeController alloc] init];
        rechargeVC.title = str;
        [self.navigationController pushViewController:rechargeVC animated:YES];
    } else if ([str isEqualToString:@"购买"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"levelupgotobuywos" object:nil];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)goTolevelUpWithInvitePhone:(NSString *)invitePhone {
    
    [SVProgressHUD show];
    
    NSString *levelNumber = [NSString stringWithFormat:@"%zd",self.level];
    
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_LevelUp parameters:@{@"level":levelNumber, @"type":self.levelUPType, @"invite_mobile":invitePhone} successBlock:^(NSDictionary *successDict) {
        @strongify(self);
        NSLog(@"%@",[NSString jsonToString:successDict]);
        [SVProgressHUD showSuccessWithStatus:successDict[@"msg"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        
        [MobClick event:@"LevelUpSuccess"]; // 友盟事件统计
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];
    
}

- (void)setLevel:(LEVELUP_TYPE)level {
    _level = level;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
